package ms.opentracing.jaeger.controller;

import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.contrib.spring.web.client.HttpHeadersCarrier;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMapAdapter;
import lombok.RequiredArgsConstructor;
import ms.opentracing.jaeger.doamin.Person;
import ms.opentracing.jaeger.service.PersonService;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RequiredArgsConstructor
@RestController
@RequestMapping(value = "person")
public class PersonController {

    private final PersonService personService;
//    private final SpanContext spanContext;
    private final Tracer tracer;
    private final StreamBridge streamBridge;

    @GetMapping(value = "{id}")
    public String getPersonById(@PathVariable(value = "id") String id, HttpServletRequest request){
        SpanContext spanContext = tracer.activeSpan().context();
        return "person";
    }

    @GetMapping(value = "one")
    public void getPerson(@RequestHeader HttpHeaders headers){
//        spanContext.baggageItems();
//       Span parentSpan = tracer.scopeManager().activeSpan();
//       Span parentSpan = tracer.activeSpan();
//       if(Objects.isNull(parentSpan)){
        Span   parentSpan = tracer.buildSpan("init-operator").start();
//            tracer.scopeManager().activate(parentSpan);
            tracer.activateSpan(parentSpan);
//       }

        parentSpan.setBaggageItem("ss","ss111");
        tracer.inject(parentSpan.context(),Format.Builtin.HTTP_HEADERS,new HttpHeadersCarrier(headers));
        parentSpan.finish();
        personService.getById();
        streamBridge.send("message-center-out-0",Person.builder().name("a").build());
    }
}
